﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;


namespace ZhimeiSoft.CharmManager.Client.Forms.Control.Custom
{
    public partial class frmCustomInfoAdd : FormBase
    {
        /// <summary>
        /// 会员信息类
        /// </summary>
        private DBEntityCustomInfo customInfo = null;
        /// <summary>
        /// 店信息
        /// </summary>
        private DataSet dsSalon = new DataSet();
        /// <summary>
        /// 当前窗体是否是编辑
        /// </summary>
        private bool editForm = false;

        public frmCustomInfoAdd(DBEntityCustomInfo aCustomInfo)
        {
            InitializeComponent();
            string error = string.Empty;
            if (BussinessProxyManager.Instance != null)
            {
                BussinessProxyManager.Instance.QuerySalonInfo(out dsSalon, out error);
                foreach (DataRow dr in dsSalon.Tables[0].Rows)
                {
                    txtSalonName.Items.Add(dr[DBEntitySalonInfo.FieldName.SNAME].ToString());
                }
                if (txtSalonName.Items.Count > 0)
                {
                    txtSalonName.SelectedIndex = 0;
                }
            }
            txtState.SelectedIndex = 0;
            txtBirthdayType.SelectedIndex = 0;
            txtBirthday.Value = DateTime.Now;
            txtRegisterDate.Value = DateTime.Now;
            txtCancelDate.Value = DateTime.Now;
            txtLastRegDate.Value = DateTime.Now;
            txtLastCallDate.Value = DateTime.Now;

            if (aCustomInfo != null)
            {
                this.customInfo = (DBEntityCustomInfo)aCustomInfo.Clone();
                //当前窗体是编辑状态
                editForm = true;
                dbEntityCustomInfoToFrom(this.customInfo);
            }
            else
            {
                //当前窗体是新增
                editForm = false;
            }
        }
        /// <summary>
        /// 初始化窗体控件状态
        /// </summary>
        /// <param name="editState"></param>
        private void initFrom(bool editState)
        {

        }

        private bool checkInput(ref DBEntityCustomInfo customInfo, out string error)
        {
            error = string.Empty;
            if (customInfo == null)
            {
                customInfo = new DBEntityCustomInfo();
            }
            //会员姓名
            if (txtCustomName.Text == string.Empty)
            {
                error = "请输入会员名称。";
                SetErrorControl(txtCustomName, "请输入会员名称");
                return false;
            }
            if (txtSalonName.Text == string.Empty)
            {
                error = "请选择会员注册店";
                SetErrorControl(txtSalonName, "请选择会员注册店。");
                return false;
            }
            return true;
        }

        private void keyPress_New(object sender, KeyPressEventArgs e)
        {
            base.keyPress(sender, e);
        }

        private void txtCustomName_TextChanged(object sender, EventArgs e)
        {

        }

        private void dateTimeInput5_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            if (!checkInput(ref customInfo, out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }
            fromToDBEntityCustomInfo(ref customInfo, out error);
            bool bState = false;
            if (editForm)
            {
                bState = BussinessProxyManager.Instance.UpdateCustomer(customInfo, out error);
            }
            else
            {
                bState = BussinessProxyManager.Instance.InsertCustomer(customInfo, out error);
               
            }
            if (!bState)
            {
                MsgBox.MsgInfo("保存会员信息错误：" + error);
                return;
            }
            this.DialogResult = DialogResult.OK;

        }
        /// <summary>
        /// 界面转换为信息类
        /// </summary>
        /// <param name="customInfo"></param>
        private bool fromToDBEntityCustomInfo(ref DBEntityCustomInfo customInfo, out string error)
        {
            error = string.Empty;
            int salonID = -1;
            DBEntitySalonInfo.getSalonNameForSalonID(dsSalon.Tables[0], txtSalonName.Text, out salonID, out error);
            
            customInfo.CNAME = txtCustomName.Text;//会员姓名
            customInfo.CNICKNAME = txtNickName.Text; //会员昵称
            customInfo.CBIRTHDAY = txtBirthday.Value;//生日
            customInfo.CSTATE = DBEntityCustomInfo.ENUM_CSTATE.ChineseStrToEnumStr(txtState.SelectedItem.ToString());//会员状态
            customInfo.CBIRTHDAYTYPE = DBEntityCustomInfo.ENUM_CSTATE.ChineseStrToEnumStr(txtBirthdayType.SelectedItem.ToString());
            customInfo.CWIGHT = txtWeight.Text; //体重
            customInfo.CHEIGHT = txtHight.Text;//身高
            customInfo.CMOBILEPHONE = txtPhone1.Text;//电话
            customInfo.CFAMLIYPHONE = txtPhone2.Text;//电话2
            customInfo.CZIPCODE = txtPostCode.Text;//邮政编码
            customInfo.CADDRESS = txtPostAddress.Text;//邮寄地址
            customInfo.CINDEXCODE = txtLocalCode.Text;//定位码
            customInfo.SALONID = salonID;//所属店ID
            customInfo.CREGISTERDATE = txtRegisterDate.Value;//注册时间
            customInfo.CANCELDATE = txtCancelDate.Value;//注销时间
            customInfo.CLASTREGISTERDATE = txtLastRegDate.Value;//最后一次到店时间
            customInfo.CLASTCALL = txtLastCallDate.Value;//最后联系时间
            customInfo.CMEMO = txtMemo.Text.Trim();//备注说明

            return true;
        }
        /// <summary>
        /// 信息类转换为界面
        /// </summary>
        /// <param name="customInfo"></param>
        private void dbEntityCustomInfoToFrom(DBEntityCustomInfo customInfo)
        {
            string salonName = string.Empty;
            string error = string.Empty;
            DBEntitySalonInfo.getSaloinIDForSalonName(dsSalon.Tables[0], customInfo.SALONID, 
                out salonName, out error);

            txtCustomName.Text = customInfo.CNAME;//会员姓名
            txtNickName.Text = customInfo.CNICKNAME; //会员昵称
            txtBirthday.Value = customInfo.CBIRTHDAY;//生日
            txtState.Text = DBEntityCustomInfo.ENUM_CSTATE.EnumStrToChineseStr(customInfo.CSTATE); //会员状态 --需要转换
            txtBirthdayType.Text = DBEntityCustomInfo.ENUM_CSTATE.EnumStrToChineseStr(customInfo.CBIRTHDAYTYPE);
            txtWeight.Text = customInfo.CWIGHT; //体重
            txtHight.Text = customInfo.CHEIGHT;//身高
            txtPhone1.Text = customInfo.CMOBILEPHONE;//电话
            txtPhone2.Text = customInfo.CFAMLIYPHONE;//电话2
            txtPostCode.Text = customInfo.CZIPCODE;//邮政编码
            txtPostAddress.Text = customInfo.CADDRESS;//邮寄地址
            txtLocalCode.Text = customInfo.CINDEXCODE;//定位码
            txtSalonName.Text = salonName;//所属店
            txtRegisterDate.Value = customInfo.CREGISTERDATE;//注册时间
            txtCancelDate.Value = customInfo.CANCELDATE;//注销时间
            txtLastRegDate.Value = customInfo.CLASTREGISTERDATE;//最后一次到店时间
            txtLastCallDate.Value = customInfo.CLASTCALL;//最后联系时间
            txtMemo.Text = customInfo.CMEMO;//备注说明
        }

        private void txtState_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (!MsgBox.MsgWarQue("是否要退出？"))
            {
                return;
            }
            DialogResult = DialogResult.Cancel;
        }

        private void frmCustomAdd_Load(object sender, EventArgs e)
        {

        }

        private void txtMemo_EditValueChanged(object sender, EventArgs e)
        {

        }

        
       
    }
}
